package com.jackrain.nea.core.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.jackrain.nea.core.api.AdSubsystemDelCmd;
import com.jackrain.nea.core.mapper.AdSubsystemMapper;
import com.jackrain.nea.core.utility.CommandData;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ValueHolder;
import com.jackrain.nea.web.query.QuerySession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "ad")
public class AdSubsystemDelCmdImpl extends CommandAdapter implements AdSubsystemDelCmd {

    @Autowired
    private AdSubsystemMapper adSubsystemMapper;

    @Override
    public ValueHolder execute(QuerySession session) throws NDSException {
        CommandData data = new CommandData(session);
        if (!data.isDeleteMainTable()) {
            throw data.notSupported();
        }

        long id = data.getObjId();
        if (adSubsystemMapper.delete(id) == 1) { //尝试删除主数据
            return data.success();
        }
        if (!adSubsystemMapper.exists(id)) { // 如果删除失败,判断id是否存在,如果存在说明还有关联数据没有删除
            return data.success();
        }
        data.putDataValue("objid", id);
        data.putDataValue("tablename", "AD_SUBSYSTEM");
        return data.error(-1, "子系统中存在表类别时无法删除，请先删除系统中的表类别！");
    }
}
